package com.ngm.tea.t.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.common.PageInfo;
import com.ngm.tea.t.dao.ModulePrivilegeDao;
import com.ngm.tea.t.entity.ModulePrivilege;
import com.ngm.tea.t.service.ModulePrivilegeService;

/**
 * 服务层
 * @author zjw
 * @version 1.0
 * @date 2016-09-01 21:07:07
 *
 */
@Service("modulePrivilegeService")
public class ModulePrivilegeServiceImpl implements ModulePrivilegeService {
	
	@Resource
	private ModulePrivilegeDao modulePrivilegeDao;

	public ModulePrivilegeDao getModulePrivilegeDao() {
		return modulePrivilegeDao;
	}

	@Override
	public int insert(ModulePrivilege modulePrivilege) {
		return modulePrivilegeDao.insert(modulePrivilege);
	}

	@Override
	public void deleteById(Integer id) {
		modulePrivilegeDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			modulePrivilegeDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(ModulePrivilege modulePrivilege) {
		modulePrivilegeDao.delete(modulePrivilege);
	}

	@Override
	public void update(ModulePrivilege modulePrivilege) {
		modulePrivilegeDao.update(modulePrivilege);
	}

	@Override
	public ModulePrivilege selectOne(Long id) {
		return modulePrivilegeDao.selectOne(id);
	}

	@Override
	public List<ModulePrivilege> selectAll() {
		return modulePrivilegeDao.selectAll();
	}

	@Override
	public List<ModulePrivilege> selectList(ModulePrivilege modulePrivilege, PageInfo pageInfo) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(modulePrivilege));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return modulePrivilegeDao.selectList(map);
	}

	@Override
	public Long count(ModulePrivilege modulePrivilege) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(modulePrivilege));
		return modulePrivilegeDao.count(map);
	}
	
	public String makeStr(ModulePrivilege modulePrivilege) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtils.isEmpty(modulePrivilege.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + modulePrivilege.getId()+"%'");
			} else {
				sb.append(" AND id LIKE '%" + modulePrivilege.getId()+"%'");
			}
		}
		if(!StringUtils.isEmpty(modulePrivilege.getModCode())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("modCode LIKE '%" + modulePrivilege.getModCode()+"%'");
			} else {
				sb.append(" AND modCode LIKE '%" + modulePrivilege.getModCode()+"%'");
			}
		}
		if(!StringUtils.isEmpty(modulePrivilege.getPriCode())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("priCode LIKE '%" + modulePrivilege.getPriCode()+"%'");
			} else {
				sb.append(" AND priCode LIKE '%" + modulePrivilege.getPriCode()+"%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}
	
}